package com.resou.reader.dialog.limitedrecharge;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.resou.common.WebViewActivity;
import com.resou.reader.R;
import com.resou.reader.activity.limitedrecharge.LimitedRechargeActivity;
import com.resou.reader.api.entry.ActivityRechargePrice;
import com.resou.reader.base.ResouBaseDialog;
import com.resou.reader.utils.PreferenceUtil;
import com.resou.reader.utils.TimeUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 阅读器页面的充值购买对话框
 */
public class LimitedRechargeDialog extends ResouBaseDialog<LimitedRechargePresenter> implements LimitedRechargeContract.View {

    @BindView(R.id.tv_hour)
    TextView tvHour;
    @BindView(R.id.tv_minute)
    TextView tvMinute;
    @BindView(R.id.tv_second)
    TextView tvSecond;
    @BindView(R.id.price_1)
    TextView price1;
    @BindView(R.id.price_2)
    TextView price2;
    private CountDownTimer mCountDownTimer;

    public static LimitedRechargeDialog newInstance() {
        Bundle args = new Bundle();
        LimitedRechargeDialog fragment = new LimitedRechargeDialog();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initPresenter() {
        mPresenter = new LimitedRechargePresenter(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.dialog_topup_purchase;
    }

    @Override
    protected void initView() {
        initData();
    }

    @Override
    public void onStart() {
        super.onStart();
        Window win = getDialog().getWindow();
        DisplayMetrics dm = new DisplayMetrics();
        // 一定要设置Background，如果不设置，window属性设置无效
        if (win != null) {
            win.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            mActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
////            win.setLayout((dm.widthPixels), (int) (dm.heightPixels * 0.5));
            WindowManager.LayoutParams params = win.getAttributes();
            params.windowAnimations = R.style.top_and_center_animation;
            params.width = (int) (dm.widthPixels * 0.8);
            win.setAttributes(params);
//            // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
        }
    }

    @OnClick({R.id.tv_dismiss, R.id.price_1, R.id.price_2, R.id.tv_activity_rule})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_dismiss:
                dismiss();
                break;
            case R.id.price_1:
                //跳转到活动页面
                LimitedRechargeActivity.start(mActivity, currentTime);
                break;
            case R.id.price_2:
                LimitedRechargeActivity.start(mActivity, currentTime);
                break;
            case R.id.tv_activity_rule:
                WebViewActivity.start(mActivity, "https://m.resouxs.com/activity/timerule", "活动规则");
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        PreferenceUtil.getInstance(mActivity).setNotShowRechargeDialogToday();
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
        }
    }

    private long currentTime;

    @Override
    public void showRemainingTime() {

    }

    @Override
    public void showPrice(List<ActivityRechargePrice> data) {
        price1.setText("充" + data.get(0).getPrice() + "元得" + +data.get(0).getActivityPrice() + "00热币");
        price2.setText("充" + data.get(1).getPrice() + "元得" + +data.get(1).getActivityPrice() + "00热币");
    }

    @Override
    public void initData() {
        mPresenter.activateRechargeActivity();
        mPresenter.loadRechargePrice(12);
        final long TIME = (long) 3 * 24 * 60 * 60 * 1000;
        //            String day;
//                String day = TimeUtil.getDay(millisUntilFinished);
        mCountDownTimer = new CountDownTimer(TIME, 1000) {
            //            String day;
            String hour;
            String minute;
            String second;

            @Override
            public void onTick(long millisUntilFinished) {
//                String day = TimeUtil.getDay(millisUntilFinished);
                String hour = TimeUtil.getAllHour(millisUntilFinished);
                String minute = TimeUtil.getStringMinute(millisUntilFinished);
                String second = TimeUtil.getStringSecond(millisUntilFinished);
                if (!hour.equals(this.hour)) {
                    this.hour = hour;
                    tvHour.setText(hour + "时");
                }
                if (!minute.equals(this.minute)) {
                    this.minute = minute;
                    tvMinute.setText(minute + "分");
                }
                if (!second.equals(this.second)) {
                    this.second = second;
                    tvSecond.setText(second + "秒");
                }
                currentTime = millisUntilFinished;
            }

            @Override
            public void onFinish() {

            }
        }.start();
    }

    @Override
    public void showContent() {

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void showError(Throwable throwable) {

    }

    @Override
    public void showEmpty() {

    }

}
